package com.woniuxy.rabc.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.rabc.dto.UserDto;
import com.woniuxy.rabc.entity.SysUser;
import com.woniuxy.rabc.service.UserService;
import com.woniuxy.rabc.util.Result;
import com.woniuxy.rabc.vo.UserVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/login")
    public Result login(@RequestBody UserDto userDto){
        SysUser sysUser = userService.selectByTelAndPwd(userDto);
        if(sysUser!=null){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @PostMapping("query")
    public Result queryLike(@RequestBody UserDto userDto){
        PageHelper.startPage(userDto.getCurrentPage(), userDto.getPageSize(), true);
        List<UserVo> sysUsers = userService.selectByLike(userDto);
        PageInfo<UserVo> pageInfo = new PageInfo<>(sysUsers);
        return Result.ok().data("list",sysUsers).data("total",pageInfo.getTotal());
    }
    @GetMapping("{id}")
    public Result queryByID(@PathVariable Long id){
        SysUser sysUser = userService.selectByPrimaryKey(id);
        return Result.ok().data("user",sysUser);
    }
    @PostMapping
    public Result add(@RequestBody SysUser sysUser){
        int i = userService.insertSelective(sysUser);
        return Result.ok();
    }
    @PutMapping
    public Result update(@RequestBody SysUser sysUser){
        int i = userService.updateByPrimaryKeySelective(sysUser);
        return Result.ok();
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id){
        int i = userService.deleteByPrimaryKey(id);
        return Result.ok();
    }
}
